import java.io.*;
import java.net.*;
import java.util.*;
import java.sql.*;
public class Server_Main{
    public static void main(String args[]){
        Scanner scanner=new Scanner(System.in);
        ServerSocket server=null;
        ServerThread thread;
        Socket socketOnServer=null;
        while(true){
        try{ server= new ServerSocket(2010);}
        catch(IOException e1){ System.out.println("正在监听");}
        try{
            System.out.println("等待客户呼叫");
            socketOnServer = server.accept();
            System.out.println("客户的地址："+socketOnServer.getInetAddress());}
        catch(Exception e){System.out.println("客户已断开"+e);}
        if(socketOnServer!=null){new ServerThread(socketOnServer).start();}    
    }
}
}
class ServerThread extends Thread{
    Scanner scanner=new Scanner(System.in);
    Socket socketOnServer;
    DataOutputStream out=null;
    DataInputStream in=null;
    String s=null;
    ServerThread(Socket t){
    socketOnServer =t;
    try {
        System.out.println("等待客户呼叫");
        out=new DataOutputStream(socketOnServer.getOutputStream());
        in=new DataInputStream(socketOnServer.getInputStream());
        for(int i=0;i<100;i++){
                String s=in.readUTF();
                System.out.println("服务器收到客户的提问："+s);
                out.writeUTF(scanner.nextLine());
        }
    }
    catch(Exception e){System.out.println("服务器已断开"+e);}
    }

}